package cn.xfdzx.android.apps.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.HotVideoAdapter;
import cn.xfdzx.android.apps.shop.adapter.VideoHeadListAllCountAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.LiveListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopTimeBean;
import cn.xfdzx.android.apps.shop.bean.VideoAvatarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.LiveListContract;
import cn.xfdzx.android.apps.shop.evenbus.LiveAllCountListMessage;
import cn.xfdzx.android.apps.shop.evenbus.VideoListMessage;
import cn.xfdzx.android.apps.shop.presenter.LiveListPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.video.VideoActivity;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author zf
 * @date 2020/3/26 0026
 * description：主播端累计观看人数fragment
 */
public class LiveAllCountFragment extends BaseMvpFragment {


    private RecyclerView rv_online_list;
    private SmartRefreshLayout mRefreshLayout;
    private VideoHeadListAllCountAdapter allCountAdapter;
    private int page = 1;
    private List<VideoAvatarListBeen> mList = new ArrayList<>();
    private LinearLayout llCarEmpty;
    private boolean isFormEventeBus = false;

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        rv_online_list = view.findViewById(R.id.rv_online_list);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        llCarEmpty = view.findViewById(R.id.ll_car_empty);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_online_list.setLayoutManager(linearLayoutManager);
        allCountAdapter = new VideoHeadListAllCountAdapter(getActivity(), mList);
        rv_online_list.setAdapter(allCountAdapter);

        String headListJson = UtilsData.getPreference(getActivity(),"roomlist").getString("headlist");
        Type type = new TypeToken<List<VideoAvatarListBeen>>() {
        }.getType();
        mList = new Gson().fromJson(headListJson,type);
        if (!isFormEventeBus){
            if (EmptyUtil.isNotEmpty(mList)){
                allCountAdapter.refreshDatas(mList);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refenceLivveAllCount(LiveAllCountListMessage messageEvent) {
        this.mList = messageEvent.getmList();
        isFormEventeBus = true;
        Log.e("videoMoreAvatarList",mList.size()+"----------");
        allCountAdapter.refreshDatas(messageEvent.getmList());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_liveallcount;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }


    @Override
    public void onError(Throwable throwable) {

    }

    public static LiveAllCountFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
//        bundle.putString("item", "UserPostFragment");
//        bundle.putLong(Constants.UID, userUid);
        LiveAllCountFragment fragment = new LiveAllCountFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {

        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

}
